#!/bin/sh

#################################################################
#  This program is the Confidential and Proprietary product of 
#  Cadence Design Systems.  Any unauthorized use, reproduction 
#  or transfer of this program is strictly prohibited. 
#  
#  Copyright (c) 1989 - 1994, Cadence Design Systems, Inc. 
#  All rights reserved. 
#  
#  Filename:  altout
#  Purpose:   script to run OSS altout netlister 
#  Author:    naji@cds9255.cadence.com
#  Date:      November 1994
#  
#  History:   
# 050195  Udi Landen	Run sed on map/current to generate the designName.map 
#			file in the sir2alt format.
#
#################################################################

#
# Get default Composer library paths
#

CDS_ROOT=`cds_root`
basicLibPath="${CDS_ROOT}/tools/dfII/etc/cdslib"
sheetLibPath="${CDS_ROOT}/tools/dfII/etc/cdslib/sheets"

#
# Get default Altera library paths
#

if [ ! "$MAXPLUS2" ]; then
	alteraLmfPath="${CDS_ROOT}/share/library/altera/lmf"
	alteraLibPath="${CDS_ROOT}/share/library/altera/cds"
else
	alteraLmfPath="${MAXPLUS2}/lmf"
	alteraLibPath="${MAXPLUS2}/simlib/composer/alt_max2"
	alteraLibPath="$alteraLibPath ${MAXPLUS2}/simlib/composer/alt_syn"
fi

#
# set program variables
#

program="`basename $0`"
version="1.0"
workDir="`pwd`"
searchPath=""
lmfFiles=""
groundList=""
powerList=""

#
# define usage funtion
#

usage_exit() {
cat << EOF >&2

Usage: $program [-device name] [-dump [paramfile]] [-family name]
       [-gnd netnames] [-help] -lib libname [-lmf lmfFile]
       [-load [paramfile]] [-log logfile] [-pwr netnames]
       [-rundir dirname] [-spath searchpath] [-view viewname]
       cellname

       -device name       Specify device name
       -dump paramfile    Dump parameters to file
       -family name       Specify device family name
       -gnd netnames      Specify global ground net names
       -help              Print usage message
       -lib libname       Specify library name
       -lmf lmfFile       Specify library map files (default: ...)
       -load paramfile    Load parameters from file
       -log logfile       Specify log file (default: ${program}.log)
       -pwr netnames      Specify global power net names 
       -rundir dirname    Specify run directiory (default: ${program}.run)
       -spath searchpath  Specify search path (default: ...)
       -view viewname     Specify view name (default: schematic)

EOF
exit 2
}

#
# define function to create full pathnames
#

full_path() {

if pathname=`dirname $1` ; then
	if filename=`basename $1` ; then
		pathname=`(cd $pathname; pwd)`
		echo $pathname/$filename
		return
	fi
fi

echo "Error: illegal file name - $1" >&2
exit 1
}

#
# process command-line arguments
#

while [ $# -gt 0 ]; do

	case $1 in

		-device)	if [ ! "$2" ]; then
						echo "Error: -device option requires an argument" >&2
						usage_exit
					fi

					deviceName=$2 ; shift ; shift ;;

		-dump)		dumpParams="yes"

					if [ "$2" ]; then
						ch=`echo $2 | awk '{ print substr( $1, 1, 1 ) }'`

						if [ "$ch" != "-" ]; then
							paramsFile=`full_path $2`
							shift
						fi
					fi ; shift ;;

		-family)	if [ ! "$2" ]; then
						echo "Error: -family option requires an argument" >&2
						usage_exit
					fi

					familyName=$2 ; shift ; shift ;;

		-gnd)		if [ ! "$2" ]; then
						echo "Error: -gnd option requires an argument" >&2
						usage_exit
					fi
 
					for gnd in `echo $2`; do
						groundList="$groundList \"$gnd\""
					done
 
					shift ; shift;;

		-help)		usage_exit ;;
		
		-lib)		if [ ! "$2" ]; then
						echo "Error: -lib option requires an argument" >&2
						usage_exit
					fi
 
					libName=$2 ; shift ; shift ;;

		-lmf)		if [ ! "$2" ]; then
						echo "Error: -lmf option requires an argument" >&2
						usage_exit
					fi

					for path in `echo $2`; do
						lmfFiles="$lmfFiles \"`full_path $path`\""
					done

					shift; shift ;;

		-load)		loadParams="yes"

					if [ "$2" ]; then
						ch=`echo $2 | awk '{ print substr( $1, 1, 1 ) }'`

						if [ "$ch" != "-" ]; then
							paramsFile=`full_path $2`
							shift
						fi
					fi ; shift ;;

		-log)		if [ ! "$2" ]; then
						echo "Error: -log option requires an argument" >&2
						usage_exit
					fi

					logFile=`full_path $2` ; shift; shift ;;

		-pwr)		if [ ! "$2" ]; then
						echo "Error: -pwr option requires an argument" >&2
						usage_exit
					fi
 
					for pwr in `echo $2`; do
						powerList="$powerList \"$pwr\""
					done
 
					shift ; shift;;

		-rundir)	if [ ! "$2" ]; then
						echo "Error: -rundir option requires an argument" >&2
						usage_exit
					fi
 
					runDir=`full_path $2` ; shift ; shift ;;

		-spath)		if [ ! "$2" ]; then
						echo "Error: -spath option requires an argument" >&2
						usage_exit
					fi

					for path in `echo $2`; do
						searchPath="$searchPath `full_path $path`"
					done

					shift ; shift ;;

		-view)		if [ ! "$2" ]; then
						echo "Error: -view option requires an argument" >&2
						usage_exit
					fi

					viewName=$2; shift ; shift ;;

		-*)			echo "Error: unrecognized option '$1'" >&2
		    		usage_exit ;;
			
		 *)			cellName=$1 ; shift ;;
	esac
done

#
# check command-line arguments
#

if [ ! "$libName" ]; then
	echo "Error: must specify library name" >&2
	usage_exit
fi

if [ ! "$cellName" ]; then
	echo "Error: must specify cell name" >&2
	usage_exit
fi

if [ ! "$viewName" ]; then
	viewName="schematic"
fi

if [ ! "$libPath" ]; then
	libPath="${workDir}"
fi

if [ ! "$runDir" ]; then
	runDir="${workDir}/${program}.run"
fi

if [ ! "$logFile" ]; then
	logFile="${workDir}/${program}.log"
fi

if [ ! "$searchPath" ]; then
	searchPath="$workDir $alteraLibPath $basicLibPath $sheetLibPath"
fi

if [ ! "$lmfFiles" ]; then
	lmfFiles="\"$alteraLmfPath/cadence.lmf\""
fi

#
# print program and copyright information
#

echo "Running $program Version $version - `date`." | tee $logFile
echo "Copyright (c) 19`date +%y` Cadence Design Systems. All Rights Reserved." | tee -a $logFile
echo "" | tee -a $logFile

#
# create and check run directory
#

if [ ! -d $runDir ]; then

	mkdir -p $runDir >/dev/null 2>&1

	if [ $? != 0 ]; then
		echo "Error: cannot create run directory - $runDir" | tee -a $logFile >&2
		exit 1
	fi

elif [ ! -w $runDir ]; then
	echo "Error: cannot write to run directory - $runDir" | tee -a $logFile >&2
	exit 1
fi

#
# go to run directory
#

cd $runDir >/dev/null 2>&1

#
# create si.env file
#

echo "simLibName = \"$libName\"" > si.env
echo "simCellName = \"$cellName\"" >> si.env
echo "simViewName = \"$viewName\"" >> si.env
echo "simLibPath = \"$searchPath" >> si.env
echo "simSimulator = \"altout\"" >> si.env
echo "simAlteraLibMapFiles = '( $lmfFiles )" >> si.env

if [ "$familyName" ]; then
	echo "simAlteraDeviceFamily = \"$familyName\"" >> si.env
fi

if [ "$deviceName" ]; then
	echo "simAlteraDeviceName = \"$deviceName\"" >> si.env
fi

if [ "$groundList" ]; then
	echo "simAlteraGndNetList = '( $groundList )" >> si.env
fi

if [ "$powerList" ]; then
    echo "simAlteraPwrNetList = '( $powerList )" >> si.env
fi

#
# create control file
#

if [ "$dumpParams" ]; then

	if [ "$paramsFile" ]; then
		echo "unless( errset( simAlteraDumpParams(\"$paramsFile\") t )" > si.control
		echo "   exit( 1 )" >> si.control
		echo ")" >> si.control
	else
		echo "unless( errset( simAlteraDumpParams() t )" > si.control
		echo "   exit( 1 )" >> si.control
		echo ")" >> si.control
	fi

	echo "" >> si.control
	echo "exit( 0 )" >> si.control

elif [ "$loadParams" ]; then

	if [ "$paramsFile" ]; then
		echo "unless( errset( simAlteraLoadParams(\"$paramsFile\") t )" > si.control
		echo "   exit( 1 )" >> si.control
		echo ")" >> si.control
	else
		echo "unless( errset( simAlteraLoadParams() t )" > si.control
		echo "   exit( 1 )" >> si.control
		echo ")" >> si.control
	fi

	echo "" >> si.control
	echo "unless( errset( netlist() t )" >> si.control
	echo "   exit( 1 )" >> si.control
	echo ")" >> si.control
	echo "" >> si.control
	echo "exit( 0 )" >> si.control

else

	echo "unless( errset( netlist() t )" > si.control
	echo "   exit( 1 )" >> si.control
	echo ")" >> si.control
	echo "" >> si.control
	echo "exit( 0 )" >> si.control
fi

#
# run si
#

if [ "$dumpParams" ]; then
	echo "Dumping default properties file ..."
else
	echo "Netlisting design ($libName $cellName $viewName) ..."
fi

si < si.control >> $logFile 2>&1

if [ $? != 0 ]; then
	echo ""
	echo "Error: failed to run netlister - Examine log file: `basename $logFile`"
	exit 1
fi

echo "" >> $logFile

#
# check the errors that cannot be trapped
#

grep "^[ \t]*ERROR:[ \t]*.*" $logFile > /dev/null 2>&1

if [ $? = 0 ]; then
	echo ""
	echo "Error: failed to run netlister - Examine log file: `basename $logFile`"
	exit 1
fi

#
# remove unnecessary files
#

/bin/rm -rf netlist

#
# translate map/current file to the sir2alt map file format
#

if [ ! "$dumpParams" ]; then

	echo  "/^[ \t]*([ \t]*inst[ \t]*$/,/^[ \t]*)/ {" > sed.map
	echo  "s/^[ \t]*([ \t]*inst/( symbolMapTable/" >> sed.map
	echo  "s/^[ \t]*)/)\\"  >> sed.map
	echo  "/"  >> sed.map
	echo  "p"  >> sed.map
	echo  "}"  >> sed.map

	echo  "/^[ \t]*([ \t]*term[ \t]*$/,/^[ \t]*)/ {" >> sed.map 
	echo  "s/^[ \t]*([ \t]*term/( pinMapTable/" >> sed.map 
	echo  "s/^[ \t]*)/)\\"  >> sed.map
	echo  "/"  >> sed.map
	echo  "p"  >> sed.map
	echo  "}"  >> sed.map

	echo  "/^[ \t]*([ \t]*net[ \t]*$/,/^[ \t]*)/ {"  >> sed.map 
	echo  "s/^[ \t]*([ \t]*net/( netMapTable/"  >> sed.map
	echo  "s/^[ \t]*)/)\\"  >> sed.map
	echo  "/"  >> sed.map
	echo  "p"  >> sed.map
	echo  "}"  >> sed.map

	sed -n -f sed.map map/current > ${cellName}.map 2>>$logFile

	if [ $? != 0 ]; then
		echo ""
		echo "Error: failed to extract map file - Examine log file: `basename $logFile`"
		exit 1
	fi

fi

#
# go back to working directory
#

cd $workDir >/dev/null 2>&1

#
# normal exit
#

echo "" | tee -a $logFile
echo "Done." | tee -a $logFile
exit 0
